package com.baichang.taoshunda.controller.mobile.business;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(ControllerConstants.BUSINESS)
public class BussinessMController extends BaseMobileController {

    @Autowired
    private BusinessService businessService;

    @Autowired
    private UserService userService;

    @Autowired
    private BussTypeService bussTypeService;

    @Autowired
    private PromotionService promotionService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private ParamsService paramsService;

    /**
     * 手机号登录
     *
     * @param request
     * @param response
     */
    @RequestMapping("/mobileMsgLogin")
    public void mobileMsgLogin(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String mobile = getString(json, "mobile", "");
                String code = getString(json, "code", "");
                return businessService.mobileMsgLogin(mobile, code);
            }
        });
    }

    /**
     * 账号登录
     *
     * @param request
     * @param response
     */
    @RequestMapping("/accountLogin")
    public void accountLogin(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String account = getString(json, "account", "");
                String pwd = getString(json, "pwd", "");
                return businessService.accountLogin(account, pwd);
            }
        });
    }

    /**
     * 注册
     *
     * @param request
     * @param response
     */
    @RequestMapping("/register")
    public void register(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String account = getString(json, "account", "");
                String code = getString(json, "code", "");
                String pwd = getString(json, "pwd", "");
                String mobile = getString(json, "mobile", "");
                String servicePhone = getString(json, "servicePhone", "");
                String name = getString(json, "name", "");
                String area = getString(json, "area", "");
                String address = getString(json, "address", "");
                Integer provinceId = getInt(json, "provinceId", 0);
                Integer cityId = getInt(json, "cityId", 0);
                Integer areaId = getInt(json, "areaId", 0);
                Integer typeId = getInt(json, "typeId", 0);
                String storePic = getString(json, "storePic", "");//门店照片
                String shopPic = getString(json, "shopPic", "");//店内照片
                String legalCardPic = getString(json, "legalCardPic", "");//法人身份证
                String handheldPhotos = getString(json, "handheldPhotos", "");//手持身份证
                String fontCard = getString(json, "fontCard", "");//身份证正面
                String reverseCard = getString(json, "reverseCard", "");//身份证反面
                String bussLicnese = getString(json, "bussLicnese", "");//营业执照
                String serviceLince = getString(json, "serviceLince", "");//服务许可证
                String legalPeople = getString(json, "legalPeople", "");//法人姓名
                String emergencyContact = getString(json, "emergencyContact", "");//紧急联系人
                String emergencyContactTel = getString(json, "emergencyContactTel", "");//紧急联系人电话
                String lng = getString(json, "lng", "");//经度
                String lat = getString(json, "lat", "");//维度
                return businessService.register(account, code, pwd, mobile, servicePhone, name, area, address, typeId, cityId, storePic, shopPic, legalCardPic, bussLicnese, serviceLince, lng, lat, handheldPhotos, fontCard, reverseCard, legalPeople, emergencyContact, emergencyContactTel, provinceId, areaId);
            }
        });
    }

    /**
     * 补全信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/authentication")
    public void authentication(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String account = getString(json, "account", "");
                String mobile = getString(json, "mobile", "");
                String servicePhone = getString(json, "servicePhone", "");
                String name = getString(json, "name", "");
                String area = getString(json, "area", "");
                String address = getString(json, "address", "");
                Integer provinceId = getInt(json, "provinceId", 0);
                Integer cityId = getInt(json, "cityId", 0);
                Integer areaId = getInt(json, "areaId", 0);
                Integer typeId = getInt(json, "typeId", 0);
                String storePic = getString(json, "storePic", "");//门店照片
                String shopPic = getString(json, "shopPic", "");//店内照片
                String legalCardPic = getString(json, "legalCardPic", "");//法人身份证
                String handheldPhotos = getString(json, "handheldPhotos", "");//手持身份证
                String fontCard = getString(json, "fontCard", "");//身份证正面
                String reverseCard = getString(json, "reverseCard", "");//身份证反面
                String bussLicnese = getString(json, "bussLicnese", "");//营业执照
                String serviceLince = getString(json, "serviceLince", "");//服务许可证
                String legalPeople = getString(json, "legalPeople", "");//法人姓名
                String emergencyContact = getString(json, "emergencyContact", "");//紧急联系人
                String emergencyContactTel = getString(json, "emergencyContactTel", "");//紧急联系人电话
                String lng = getString(json, "lng", "");//经度
                String lat = getString(json, "lat", "");//维度
                return businessService.authentication(account, mobile, servicePhone, name, area, address, typeId, cityId, storePic, shopPic, legalCardPic, bussLicnese, serviceLince, lng, lat, handheldPhotos, fontCard, reverseCard, legalPeople, emergencyContact, emergencyContactTel, provinceId, areaId);
            }
        });
    }

    /**
     * 忘记密码修改密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateForgetPwd")
    public void updateForgetPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String account = getString(json, "account", "");
                String newPwd = getString(json, "newPwd", "");
                return businessService.updateForgetPwd(account, newPwd);
            }
        });
    }

    /**
     * 获取个人信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getBussinessInfo")
    public void getBussinessInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                return businessService.getBussinessInfo(bussId);
            }
        });
    }

    /**
     * 修改商家个人信息
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateBussinessInfo")
    public void updateBussinessInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String headPic = getString(json, "headPic", "");
                String name = getString(json, "name", "");
                String hxKid = getString(json, "hxKid", "");
                String sex = getString(json, "sex", "男");
//                Integer cityId= getInt(json, "cityId", 0);
//                String area= getString(json, "area", "");
//                String address = getString(json, "address", "");
//                String lat = getString(json, "lat", "");
//                String lng = getString(json, "lng", "");
                return businessService.updateBusinessInfo(bussId, headPic, name, hxKid, sex);
            }
        });
    }

    /**
     * 获取更换账号时信手机号的验证码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getChangeAccountOldMobileMsg")
    public void getChangeAccountOldMobileMsg(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                return businessService.getChangeAccountOldMobileMsg(bussId);
            }
        });
    }

    /**
     * 获取更换账号时新手机号的验证码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getChangeAccountNewMobileMsg")
    public void getChangeAccountNewMobileMsg(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String mobile = getString(json, "mobile", "");
                return businessService.getChangeAccountNewMobileMsg(mobile);
            }
        });
    }

    /**
     * 修改账号
     *
     * @param request
     * @param response
     */
    @RequestMapping("/changeAccount")
    public void changeAccount(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String newAccount = getString(json, "newAccount", "");
                return businessService.changeAccount(bussId, newAccount);
            }
        });
    }

    /**
     * 检查密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/checkPwd")
    public void checkPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String pwd = getString(json, "pwd", "");
                return businessService.checkPwd(bussId, pwd);
            }
        });
    }

    /**
     * 修改密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updatePwd")
    public void updatePwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String pwd = getString(json, "pwd", "");
                String surePwd = getString(json, "surePwd", "");
                return businessService.updatePwd(bussId, pwd, surePwd);
            }
        });
    }

    /**
     * 开始营业
     *
     * @param request
     * @param response
     */
    @RequestMapping("/openShop")
    public void openShop(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                return businessService.updateShopIsOpen(bussId, 1);
            }
        });
    }

    /**
     * 停止营业
     *
     * @param request
     * @param response
     */
    @RequestMapping("/closeShop")
    public void closeShop(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                return businessService.updateShopIsOpen(bussId, 2);
            }
        });
    }

    /**
     * 修改商家的背景图片
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateBussinessBackgroundPic")
    public void updateBussinessBackgroundPic(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String backgroundPic = getString(json, "backgroundPic", "");
                return businessService.updateBussinessBackgroundPic(bussId, backgroundPic);
            }
        });
    }

    /**
     * 获取信息
     */
    @RequestMapping("/getUserInfoByTrendsUserId")
    public void getUserInfoByTrendsUserId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                int userId = getInt(json, "userId", 1);
                return userService.getUserInfoByTrendsUserId(userId);
            }
        });
    }

    /**
     * 查询商家类别
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findBusinessType")
    public void findBusinessType(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer cityId = getInt(json, "cityId", 0);
                return bussTypeService.getCompanyType(cityId);
            }
        });
    }

    /**
     * 查看优惠活动
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findPromotionByState")
    public void findPromotionByState(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer state = getInt(json, "state", 0);
                return promotionService.findPromotionByState(bussId, state);
            }
        });
    }

    /**
     * 创建优惠活动
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addPromotion")
    public void addPromotion(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String name = getString(json, "name", "");
                String beginTime = getString(json, "beginTime", "");
                String endTime = getString(json, "endTime", "");
                return promotionService.addPromotion(bussId, name, beginTime, endTime);
            }
        });
    }

    /**
     * 查看商家详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getBussDetails")
    public void getBussDetails(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                return businessService.getBussDetails(id);
            }
        });
    }

    /**
     * 修改商家详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateBussDetails")
    public void updateBussDetails(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer id = getInt(json, "id", 0);
                String startTime = getString(json, "startTime", "");
                String endTime = getString(json, "endTime", "");
                String phone = getString(json, "phone", "");
                String area = getString(json, "area", "");
                Integer cityId = getInt(json, "cityId", 0);
                Integer areaId = getInt(json, "areaId", 0);
                Integer provinceId = getInt(json, "provinceId", 0);
                String address = getString(json, "address", "");
                String lat = getString(json, "lat", "");
                String lng = getString(json, "lng", "");
                String isInvoice = getString(json, "isInvoice", "");//淘瞬达二期
                String storePic = getString(json, "storePic", "");//淘瞬达二期
                String shopPic = getString(json, "shopPic", "");//淘瞬达二期
                String otherLicenses = getString(json, "otherLicenses", "");//淘瞬达二期  其他许可证
                String otherLicensesTwo = getString(json, "otherLicensesTwo", "");//淘瞬达二期 其他许可证2
                String bussLicnese = getString(json, "bussLicnese", "");//营业执照
                String serviceLince = getString(json, "serviceLince", "");//服务许可证
                Integer isDispatching = getInt(json, "isDispatching", 0);
                return businessService.updateBussDetails(id, startTime, endTime, phone, area, cityId, address, lat, lng, isDispatching, isInvoice, storePic, shopPic, provinceId, areaId, otherLicenses, otherLicensesTwo, bussLicnese, serviceLince);
            }
        });
    }


    /**
     * 查看优惠活动
     *
     * @param request
     * @param response
     */
    @RequestMapping("/findCouponByState")
    public void findCouponByState(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                Integer state = getInt(json, "state", 0);
                return couponService.findCouponByState(bussId, state);
            }
        });
    }

    /**
     * 创建优惠活动
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addCoupon")
    public void addCoupon(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String name = getString(json, "name", "");
                String beginTime = getString(json, "beginTime", "");
                String endTime = getString(json, "endTime", "");
                Integer days = getInt(json, "days", 0);
                String jsonData = getString(json, "jsonData", "[]");
                return couponService.addCoupon(bussId, name, beginTime, endTime, days, jsonData);
            }
        });
    }

    /**
     * 联系客服
     *
     * @param request
     * @param response
     */
    @RequestMapping("/contactService")
    public void getAboutInfo(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                Map map = new HashMap();
                map.get("key");
                return paramsService.getParamsValue(21, 33);
            }
        });
    }

    /**
     * 添加取现密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/addCashPwd")
    public void addCashPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String cashPwd = getString(json, "cashPwd", "");
                return businessService.addCashPwd(bussId, cashPwd);
            }
        });
    }

    /**
     * 修改取现密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/updateCashPwd")
    public void updateCashPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                String cashPwd = getString(json, "cashPwd", "");
                return businessService.addCashPwd(bussId, cashPwd);
            }
        });
    }

    /**
     * 获取提现密码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getCashPwd")
    public void getCashPwd(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer bussId = getInt(json, "bussId", 0);
                return businessService.getCashPwd(bussId);
            }
        });
    }

    /**
     * 商家注册第一步
     * 填写手机号验证码
     *
     * @param request
     * @param response
     */
    @RequestMapping("/registerFirstStep")
    public void registerFirst(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                String account = getString(json, "account", "");
                String code = getString(json, "code", "");
                String pwd = getString(json, "pwd", "");
                Integer userId = getInteger(json, "userId", 0);
                Integer registerNumber = getInteger(json, "registerNumber", 1);
                return businessService.businessregister(account, code, pwd, userId, registerNumber);
            }
        });
    }

    /**
     * 商家注册第二步
     * 保存
     *
     * @param request
     * @param response
     */
    @RequestMapping("/registerSecondStep")
    public void registerSecondStep(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {

                Integer companyId = getInteger(json, "companyId", 0);
                Integer registerNumber = getInteger(json, "registerNumber", 2);
                String servicePhone = getString(json, "servicePhone", "");
                String name = getString(json, "name", "");
                String area = getString(json, "area", "");
                String address = getString(json, "address", "");
                Integer provinceId = getInt(json, "provinceId", 0);
                Integer cityId = getInt(json, "cityId", 0);
                Integer areaId = getInt(json, "areaId", 0);
                Integer typeId = getInt(json, "typeId", 0);
                String lat = getString(json, "lat", "");
                String lng = getString(json, "lng", "");
                //法人姓名
                String legalPeople = getString(json, "legalPeople", "");
                //紧急联系人
                String emergencyContact = getString(json, "emergencyContact", "");
                //紧急联系人电话
                String emergencyContactTel = getString(json, "emergencyContactTel", "");
                return businessService.registerSecondStep(companyId, area, provinceId, registerNumber, servicePhone, name, address, cityId, areaId, typeId, lat, lng, legalPeople, emergencyContact, emergencyContactTel);
            }
        });
    }

    /**
     * 商家注册第三步
     * 保存图片
     *
     * @param request
     * @param response
     */
    @RequestMapping("/registerThreeStep")
    public void registerThreeStep(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest request, HttpServletResponse response, String json) throws ServiceException {
                Integer companyId = getInteger(json, "companyId", 0);
                Integer registerNumber = getInteger(json, "registerNumber", 3);
                //门店照片
                String storePic = getString(json, "storePic", "");
                //店内照片
                String shopPic = getString(json, "shopPic", "");
                //法人身份证
                String legalCardPic = getString(json, "legalCardPic", "");
                //手持身份证
                String handheldPhotos = getString(json, "handheldPhotos", "");
                //身份证正面
                String fontCard = getString(json, "fontCard", "");
                //身份证反面
                String reverseCard = getString(json, "reverseCard", "");
                //营业执照
                String bussLicnese = getString(json, "bussLicnese", "");
                //服务许可证
                String serviceLince = getString(json, "serviceLince", "");
                return businessService.registerThreeStep(companyId, registerNumber, storePic, shopPic, legalCardPic, handheldPhotos, fontCard, reverseCard, bussLicnese, serviceLince);
            }
        });
    }
}
